package org.itcast.adi.webTemplate.service.impl;

import org.itcast.adi.planinfo.entity.PlanInfo;
import org.itcast.adi.planinfo.entity.ResultBean;
import org.itcast.adi.webTemplate.entity.ResTemplateParam;
import org.itcast.adi.webTemplate.entity.TianGouPo;
import org.itcast.adi.webTemplate.service.RestTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RestTemplateServiceImpl implements RestTemplateService {
    @Autowired
    private RestTemplate restTemplate;

    @Override
    public ResultBean get() {
//        传参可以用占位符，map封装参数有可能会报错（有疑问），尽量参数拼接在参数上面
        String url = "https://apis.tianapi.com/tiangou/index?key=4556c57375b2fcda12f6fc50f8570558";
        ResponseEntity<TianGouPo> entity = restTemplate.getForEntity(url, TianGouPo.class);
        System.out.println(entity);
        return ResultBean.success(entity.getBody());
    }

    @Override
    public ResultBean post() {
        String url = "https://apis.tianapi.com/tiangou/index";
//        基本类型传参和实体传参
        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();
        paramMap.add("key", "4556c57375b2fcda12f6fc50f8570558");
        ResponseEntity<TianGouPo> entity = restTemplate.postForEntity(url, paramMap, TianGouPo.class);
        System.out.println(entity);
        return ResultBean.success(entity.getBody());
    }

    @Override
    public ResultBean post2() {
        String url = "http://localhost:8080/planinfo/findAll";
        // 申请一个请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        /*这里不能使用MultiValueMap，不然会报错*/
        ResTemplateParam resTemplateParam = new ResTemplateParam();
        resTemplateParam.setPlanDesc("test");
        // 使用HttpEntity形式包装
        HttpEntity<ResTemplateParam> httpEntity = new HttpEntity<>(resTemplateParam, headers);
        ResponseEntity entity = restTemplate.postForEntity(url, httpEntity, Object.class);
        Map body = (Map) entity.getBody();
        List<PlanInfo> planInfoList = (List<PlanInfo>) body.get("datas");
        return ResultBean.success(entity.getBody());
    }

    @Override
    public ResultBean exchange() {
        String url = "http://localhost:8080/planinfo/findAll";
        // 申请一个请求头
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        ResTemplateParam resTemplateParam = new ResTemplateParam();
        resTemplateParam.setPlanDesc("test");
        // 使用HttpEntity形式包装
        HttpEntity<ResTemplateParam> httpEntity = new HttpEntity<>(resTemplateParam, headers);
        ResponseEntity<Object> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, Object.class);
        System.out.println(exchange.getBody());
        return ResultBean.success(exchange.getBody());
    }

    @Override
    public ResultBean exchange1() {
        String url = "http://localhost:8080/planinfo/findById?planId="+"1";

        HttpHeaders headers = new HttpHeaders();
        headers.add("Connection","keep-alive");
        HttpEntity<Map<String, Object>> entity = new HttpEntity<>(headers);


        ResponseEntity exchange = restTemplate.exchange(url, HttpMethod.GET, entity, Object.class);
        System.out.println(exchange.getBody());


        ParameterizedTypeReference<ResultBean<PlanInfo>> responseBodyType = new ParameterizedTypeReference<ResultBean<PlanInfo>>() {};
        ResponseEntity exchange1 = restTemplate.exchange(url, HttpMethod.GET, null, responseBodyType);
        System.out.println(exchange1.getBody());
        return ResultBean.success(exchange.getBody());
    }

    @Override
    public ResultBean exchange2() {
        String url = "http://localhost:8080/planinfo/findById/0?planDesc="+"test";
        ResponseEntity exchange = restTemplate.exchange(url, HttpMethod.GET, null, Object.class);
        System.out.println(exchange.getBody());
        Map map = (Map) exchange.getBody();
        List<PlanInfo> planInfos = (List<PlanInfo>) map.get("datas");

        ParameterizedTypeReference<ResultBean<List<PlanInfo>>> responseBodyType = new ParameterizedTypeReference<ResultBean<List<PlanInfo>>>() {};
        ResponseEntity exchange1 = restTemplate.exchange(url, HttpMethod.GET, null, responseBodyType);
        System.out.println(exchange1.getBody());
        return ResultBean.success(exchange1.getBody());
    }

    @Override
    public ResultBean exchange3() {
        String url = "http://localhost:8080/planinfo/findAllOne/1";
        PlanInfo planInfo = new PlanInfo();
        planInfo.setPlanDesc("test");
        // headers
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type","application/json");
        HttpEntity<PlanInfo> httpEntity = new HttpEntity<>(planInfo, headers);
        ResponseEntity<Object> exchange = restTemplate.exchange(url, HttpMethod.POST, httpEntity, Object.class);

        HashMap<String,Object> map = new HashMap<>();
        map.put("planDesc","test");
        HttpEntity<HashMap<String, Object>> mapHttpEntity = new HttpEntity<>(map, headers);
        ResponseEntity<Object> exchanged = restTemplate.exchange(url, HttpMethod.POST, mapHttpEntity, Object.class);

//        自定义泛型类型
        ParameterizedTypeReference<ResultBean<List<PlanInfo>>> responseBodyType = new ParameterizedTypeReference<ResultBean<List<PlanInfo>>>() {};
        ResponseEntity<ResultBean<List<PlanInfo>>> exchange1 = restTemplate.exchange(url, HttpMethod.POST, httpEntity, responseBodyType);
        System.out.println(exchange1.getBody());



        System.out.println(exchange.getBody());
        return ResultBean.success(exchange.getBody());
    }
}
